package com.dayaokeji.rhythmschoolstudent.utils;

import android.text.TextUtils;

import com.dayaokeji.server_api.domain.Course;
import com.dayaokeji.server_api.domain.Meeting;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Function;

// Created by Zeno on 2018/5/22
// version 1.0
// Copyright (C) 2018 -- 2020 ALL Right
public class WebTimeHandle {

    public static Observable checkSignStatus(final Course course) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                TimeUtils.webDateTime(new TimeUtils.RequestWebDateTimeListener() {
                    @Override
                    public void requestDateTime(String webDateTime) {
                        emitter.onNext(webDateTime);
                    }
                });
            }
        }).map(new Function<String,Integer>() {

            @Override
            public Integer apply(String s) throws Exception {
                return TimeUtils.compareDate(s, course.getActStartTime(), course.getSignStartTime(), course.getSignEndTime());
            }
        });
    }

    public static Observable checkSignStatus(final Meeting meeting) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                TimeUtils.webDateTime(new TimeUtils.RequestWebDateTimeListener() {
                    @Override
                    public void requestDateTime(String webDateTime) {
                        emitter.onNext(webDateTime);
                    }
                });
            }
        }).map(new Function<String,Integer>() {

            @Override
            public Integer apply(String s) throws Exception {
                return TimeUtils.compareDate(s, meeting.getActStartTime(), meeting.getSignStartTime(), meeting.getSignEndTime());
            }
        });
    }

    public static Observable<String> getSignDateTime(final Course course) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                TimeUtils.webDateTime(new TimeUtils.RequestWebDateTimeListener() {
                    @Override
                    public void requestDateTime(String webDateTime) {
                        if (!TextUtils.isEmpty(webDateTime)) {
                            emitter.onNext(webDateTime);
                        }
                    }
                });
            }
        }).map(new Function<String,String>() {

            @Override
            public String apply(String s) throws Exception {
                return TimeUtils.getCompareDateTime(s, course.getActStartTime(), course.getSignStartTime(), course.getSignEndTime()) == null ? "" : TimeUtils.getCompareDateTime(s, course.getActStartTime(), course.getSignStartTime(), course.getSignEndTime());
            }
        });
    }

    public static Observable<String> getSignDateTime(final Meeting meeting) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                TimeUtils.webDateTime(new TimeUtils.RequestWebDateTimeListener() {
                    @Override
                    public void requestDateTime(String webDateTime) {
                        emitter.onNext(webDateTime);
                    }
                });
            }
        }).map(new Function<String,String>() {

            @Override
            public String apply(String s) throws Exception {
                return TimeUtils.getCompareDateTime(s, meeting.getActStartTime(), meeting.getSignStartTime(), meeting.getSignEndTime()) == null? "" : TimeUtils.getCompareDateTime(s, meeting.getActStartTime(), meeting.getSignStartTime(), meeting.getSignEndTime());
            }
        });
    }
}
